package com.example.demo.controller;

import com.example.demo.Dao.sc_userRepository;
import com.example.demo.admin.bean.AdminBean;
import com.example.demo.admin.repository.AdminRepository;
import com.example.demo.navigation.bean.NavBean;
import com.example.demo.security.bean.SecurityResourceBean;
import com.example.demo.security.service.SecurityResourceServer;
import com.example.demo.security.bean.SecurityRoleBean;
import com.example.demo.security.service.SecurityRoleService;
import com.example.demo.security.bean.SecurityRoleResourceBean;
import com.example.demo.security.repository.SecurityRoleResourceRepository;
import com.example.demo.navigation.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("/")
public class DemoController {



    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    public sc_userRepository repository;

    @Autowired
    private NavService navService;


    @Autowired
    private SecurityRoleService roleService;

    @Autowired
    private SecurityResourceServer securityResourceServer;

    @Autowired
    private SecurityRoleResourceRepository roleResourceRepository;

    @RequestMapping(method = RequestMethod.GET)
    public String test(){
        System.out.println("test");
        return "index";
    }

    @RequestMapping("index")
    public ModelAndView index(ModelAndView modelAndView){
        System.out.println(navService.getList(0).size());
        modelAndView.addObject("navs",navService.getList(0));

        modelAndView.setViewName("test");
        return modelAndView;
    }

    @RequestMapping("list")
    public ModelAndView getBookList(ModelAndView modelAndView){
        List<AdminBean> list = adminRepository.findAll();
        System.out.println("list ---"+list.size());
        System.out.println("list count:"+adminRepository.count());
        System.out.println("list count:"+repository.count());
        for (AdminBean adminBean : list) {
            System.out.println(adminBean.getUsername());
        }
        modelAndView.addObject("admins",list);
        modelAndView.setViewName("booklist");

        return modelAndView;
    }


    @RequestMapping("config")
    public ModelAndView config(ModelAndView modelAndView){
        modelAndView.addObject("navtype1",navService.getList(0));
        modelAndView.addObject("navtype2",navService.getList(1));
        modelAndView.addObject("navtype3",navService.getList(2));
        modelAndView.setViewName("config");
        modelAndView.addObject("entity",new NavBean());
        return modelAndView;
    }

    @RequestMapping("nav")
    @ResponseBody
    public List<NavBean> navTest(){
        return navService.getList(1);
    }

    @RequestMapping("nav/{id}")
    @ResponseBody
    public List<NavBean> navById(@PathVariable int id){
        return navService.getList(1,id);
    }

    @RequestMapping(value = "addNav",method = RequestMethod.POST)
    public String addNav(NavBean entity){
        System.out.println(entity.getTitle());
        navService.addEntity(entity);
        return "redirect:/config";
    }

    @RequestMapping(value = "delNav",method = RequestMethod.GET)
    public String delNav(int id){
        navService.delEntity(id);
        return "redirect:/config";
    }


    @RequestMapping(value = "securitylist")
    public String securityList(Model model){
        model.addAttribute("resourcelist",securityResourceServer.getAll());
        model.addAttribute("rolelist",roleService.getAll());
        return "security";
    }


    @RequestMapping(value = "showrole/{id}")
    public String roleDetails(@PathVariable int id,Model model){

//        List<SecurityResourceBean> list = new ArrayList<>();
//        Iterator<SecurityRoleResourceBean> iterable = roleService.getById(id).getRoleResourceBeanSet().iterator();
//        while (iterable.hasNext())
//        {
//            list.add(iterable.next().getResource());
//        }
//        model.addAttribute("resourcelist",list);
        model.addAttribute("resourcelist",roleService.getById(id).getRoleResourceBeanSet());
        return "roledetails";
    }

    @RequestMapping(value = "security")
    public String securit(){
        System.out.println("Security......");
        SecurityRoleBean bean = new SecurityRoleBean();
        bean.setRoleName("ROLL_TEST");
        bean.setNickName("测试权限");
        roleService.save(bean);

        SecurityResourceBean resBean = new SecurityResourceBean();
        resBean.setResourceName("资源名称");
        resBean.setResourceUrl("/Admin/**");
        securityResourceServer.save(resBean);


        SecurityRoleResourceBean roleResourceBean = new SecurityRoleResourceBean();
        roleResourceBean.setRole(bean);
        roleResourceBean.setResource(resBean);
        roleResourceRepository.save(roleResourceBean);

//        Set<SecurityResourceBean> set = new HashSet<>();
//        set.add(resBean);
//        bean.setResourceBeans(set);
//        roleService.save(bean);
        return "redirect:/config";
//        System.out.println(securityResourceServer.getAll().size());
    }


}
